/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-03-06 09:33:34
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-20 13:35:45
 */
import {
  orderAPI
} from "../../../api/index"
import {
  returnStatus,
  returnStatusToast,
  afterNav,
  payOrder
} from '../../../utils/util'

const titles = ["全部", "待付款", "待发货", "待收货", "待评价", "退款/售后"]
var appInst = getApp();
// pages/mine/order/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tab: 0,
    confirmDel: false,
    pageNum: 1,
    requestObj: {},
    records: [],
    showData: false,
    loadFinish: false,
  },
  // 获取订单数据
  getData(orderStatus, pageSize = 10) {
    this.setData({
      loadFinish: false
    })
    return new Promise(async (resolve, reject) => {
      const api = this.tab === 5 ? 'queryAfterList' : 'queryOrderList'
      const res = await orderAPI[api]({
        pageNum: this.data.pageNum,
        orderStatus: orderStatus || this.tab || undefined,
        pageSize
      })
      const {
        records
      } = await returnStatus(res)
      if (records) {
        this.setData({
          records: [...this.data.records, ...records],
          pageNum: ++this.data.pageNum,
          pages: records.length,
          showData: true,
        })
        setTimeout(() => {
          this.setData({
            loadFinish: true
          })
        }, 300);
        resolve(records.length)
      }
      reject(false)

    })
  },
  toOrderDetail({
    currentTarget: {
      dataset: {
        orderid,
        info,
        info: {
          status
        }
      }
    }
  }) {
    if (this.tab === 5) {
      return afterNav(info, status)
    }
    wx.navigateTo({
      url: `/pages/queryOrder/index?orderId=${orderid}&type=detail`,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({
    tab
  }) {
    var appInst = getApp();
    this.setData({title:'我的订单-' + titles[tab]})
    appInst.setTitle('我的订单-' + titles[tab])
    tab *= 1
    this.tab = tab
    this.setData({
      tab: tab,
      'requestObj': {
        fetchData: (refresh) => {
          refresh ? this.setData({
            pageNum: 1,
            records: []
          }) : ''
          return this.getData()
        },
      },
    })
    if (tab) return
    this.getData()
  },

  // 查看物流
  showLogistics({
    target: {
      dataset: {
        orderid
      }
    }
  }) {
    wx.navigateTo({
      url: `/pages/mine/order/logistics/package/index?orderId=${orderid}`,
    });
  },
  async goPay({
    target: {
      dataset: {
        orderid,
        code
      }
    }
  }) {
    const res = await payOrder(orderid, code)
  },
  // 取消订单
  async cancelOrder() {
    const api = {
      1: 'cancelOrder',
      3: 'confirmOrderFinish'
    }
    const res = await orderAPI[api[this.tab]]({
      orderId: this.orderId
    })
    returnStatusToast(res, () => {
      if (this.tab === 3) {
        // 跳转到交易成功
        wx.navigateTo({
          url: '/pages/mine/order/success/index?orderIndex'
        })
        return
      }
      this.onChangeTab({
        detail: {
          name: 1
        }
      })
    })
  },
  showDialog({
    target: {
      dataset: {
        orderid
      }
    }
  }) {
    this.orderId = orderid
    this.setData({
      confirmDel: true
    })
  },
  onChangeTab({
    detail: {
      name
    }
  }) {
    this.tab = name
    this.setData({
      pageNum: 1,
      records: [],
      showData: false,
      tab: name
    })
    let title = '我的订单-' + titles[name]
    this.setData({'navbarData.title':title})
    this.getData(name || undefined)
    appInst.setTitle(title)
  },
  toEvaluate({
    target: {
      dataset: {
        info
      }
    }
  }) {
    wx.navigateTo({
      url: '/pages/mine/order/evaluate/index',
      success: (res) => {
        res.eventChannel.emit('params', {
          info
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})